package com.example.contextcube.util;

import android.content.Context;
import android.content.SharedPreferences;
import android.content.res.Configuration;
import android.content.res.Resources;
import android.os.Build;
import android.os.LocaleList;
import android.util.Log;

import androidx.preference.PreferenceManager;

import java.util.Locale;

/**
 * 语言环境管理器
 * 用于处理应用内的语言切换
 */
public class LocaleManager {
    private static final String TAG = "LocaleManager";
    private static final String LANGUAGE_KEY = "language";
    
    // 支持的语言代码
    public static final String LANGUAGE_CHINESE = "zh";
    public static final String LANGUAGE_ENGLISH = "en";
    
    // 默认使用中文
    private static final String DEFAULT_LANGUAGE = LANGUAGE_CHINESE;
    
    private static LocaleManager instance;
    
    private LocaleManager() {
        // 私有构造函数
    }
    
    /**
     * 获取单例实例
     */
    public static synchronized LocaleManager getInstance() {
        if (instance == null) {
            instance = new LocaleManager();
        }
        return instance;
    }
    
    /**
     * 设置应用语言
     * @param context 上下文
     * @param language 语言代码 (zh, en)
     * @return 更新后的上下文
     */
    public Context setLocale(Context context, String language) {
        persistLanguage(context, language);
        return updateResources(context, language);
    }
    
    /**
     * 获取当前设置的语言
     * @param context 上下文
     * @return 语言代码
     */
    public String getLanguage(Context context) {
        SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(context);
        return preferences.getString(LANGUAGE_KEY, DEFAULT_LANGUAGE);
    }
    
    /**
     * 保存语言设置到SharedPreferences
     * @param context 上下文
     * @param language 语言代码
     */
    private void persistLanguage(Context context, String language) {
        SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(context);
        SharedPreferences.Editor editor = preferences.edit();
        editor.putString(LANGUAGE_KEY, language);
        editor.apply();
    }
    
    /**
     * 更新资源配置
     * @param context 上下文
     * @param language 语言代码
     * @return 更新后的上下文
     */
    private Context updateResources(Context context, String language) {
        Locale locale = new Locale(language);
        Locale.setDefault(locale);
        
        Resources resources = context.getResources();
        Configuration configuration = new Configuration(resources.getConfiguration());
        
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
            LocaleList localeList = new LocaleList(locale);
            LocaleList.setDefault(localeList);
            configuration.setLocales(localeList);
            context = context.createConfigurationContext(configuration);
        } else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
            configuration.setLocale(locale);
            context = context.createConfigurationContext(configuration);
        } else {
            configuration.locale = locale;
            resources.updateConfiguration(configuration, resources.getDisplayMetrics());
        }
        
        return context;
    }
    
    /**
     * 获取当前系统的默认语言
     * @return 语言代码
     */
    public String getSystemLanguage() {
        String language = Locale.getDefault().getLanguage();
        // 如果系统语言不是我们支持的语言，则返回默认语言
        if (language.equals(LANGUAGE_CHINESE) || language.equals(LANGUAGE_ENGLISH)) {
            return language;
        }
        return DEFAULT_LANGUAGE;
    }
} 